package com.cyzy.controller;

import com.cyzy.dto.Result;
import com.cyzy.pojo.AdminUser;
import com.cyzy.pojo.Passenger;
import com.cyzy.pojo.RoleMenu;
import com.cyzy.service.UserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    //对用户进行分页查询
    @GetMapping("/page")
    public Result userPageHelper(int page, int size){
        // 启动分页，page表示当前页，size表示每页的记录数
//        PageHelper.startPage(page, size);
        PageHelper.startPage(page,size);
        // 获取分页后的数据
        List<Passenger> userList = userService.selectUserList();
        if (userList.isEmpty()){
            return Result.error().message("暂无数据！");
        }

        PageInfo<Passenger> pageInfo = new PageInfo<>(userList);
//        System.out.println(adminMenuList);
//        System.out.println(pageInfo);
        // 返回分页后的数据列表
        return Result.ok().data("list",userList).data("count",pageInfo.getTotal());
    }

    //搜索模块的查询
    @GetMapping("/searchuserlist")
    public Result searchuserlist(Passenger passenger) {
        // 处理参数，防止 SQL 语句出错
        if ((passenger.getName() == null || passenger.getName().isEmpty()) &&
                (passenger.getPhone() == null || passenger.getPhone().isEmpty()) &&
                (passenger.getUserAcc() == null || passenger.getUserAcc().isEmpty()) &&
                (passenger.getGender() == null || passenger.getGender().isEmpty()) &&
                (passenger.getIdCard() == null || passenger.getIdCard().isEmpty()) &&
                (passenger.getUserStatus() == null)) {
            return Result.error().message("请至少输入一个搜索条件");
        }

        List<Passenger> passengerList = userService.searchUserList(passenger.getName(),passenger.getPhone(),
                passenger.getUserAcc(),passenger.getGender(),passenger.getIdCard(),passenger.getUserStatus());

        if (passengerList != null && !passengerList.isEmpty()) {
            PageInfo<Passenger> pageInfo = new PageInfo<>(passengerList);
            // 返回分页后的数据列表
            return Result.ok().data("list",passengerList).data("count",pageInfo.getTotal());
        }

        return Result.error().message("未找到符合条件的用户！");
    }

    //对用户进行修改
    @PutMapping("/update")
    public Result updateManage(@RequestBody Passenger passenger){
//        System.out.println(passenger + " " + "262348513516848");

        int res = userService.updateManage(passenger.getUserId(),passenger.getName(),passenger.getPhone(),passenger.getUserAcc(),
                passenger.getGender(),passenger.getIdCard(),passenger.getBalance(),passenger.getUserStatus(),passenger.getAvatar());
        if (res == 0){
            return Result.error().message("修改失败！");
        }
        return Result.ok().message("修改成功!");
    }

    //对用户进行逻辑删除
    @DeleteMapping("/delete")
    public Result deleteManage(@RequestBody Passenger passenger){
//        System.out.println(passenger + " " + "561535461");

        int res = userService.deleteState(passenger.getUserId(),passenger.getUserStatus());

        if(res != 1){
            return Result.error().message("删除失败！");
        }
        return Result.ok().message("删除成功！");
    }

    //对用户进行逻辑批量删除
    @DeleteMapping("/delete1")
    public Result deleteManage1(@RequestParam("userStatus") String state,@RequestParam("userId") List<String> ids){

        int res = userService.deleteStateByIds(ids, state); // 调用批量逻辑删除方法

        if (res == 0) {
            return Result.error().message("批量删除失败！");
        }
        return Result.ok().message("成功删除 " + res + " 条记录！");
    }
}

